package com.school_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.school_admin.config.Result;
import com.school_admin.entity.User;
import com.school_admin.mapper.UserMapper;
import com.school_admin.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-03-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<Map<String, Integer>> getCount(String uid) {
        int like = userMapper.getLikeCount(uid);
        int collection = userMapper.getCollectionCount(uid);
        int publish = userMapper.getPublishCount(uid);
        Map<String, Integer> map = new HashMap<>();
        map.put("like", like);
        map.put("collection", collection);
        map.put("publish", publish);
        return Result.success(map);
    }

    @Override
    public Result<?> getUserCollectionList(int page, int pageSize, String uid) {
        int start = (page - 1) * pageSize;
        List<Map<String, Object>> list = userMapper.getUserCollectionList(start, pageSize, uid);
        if (list.size() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", list);
            map.put("total", userMapper.getCollectionCountByUid(uid));
            return Result.success(map);
        }
        return Result.fail("没有收藏");
    }

    @Override
    public Result<Map> searchUser(int page, int pageSize, String username,String role,String uid) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(User::getUsername, username);
        wrapper.eq(User::getRole, role);
        wrapper.ne(User::getUid, uid);
        Page<User> userPage = new Page<>(page, pageSize);
        IPage<User> userIPage = userMapper.selectPage(userPage, wrapper);
        if (userIPage.getTotal() > 0) {
            Map<String,Object> map = new HashMap<>();
            map.put("list",userIPage.getRecords());
            map.put("total",userIPage.getTotal());
            return Result.success(map);
        }
        return Result.fail("没有找到用户");
    }

    @Override
    public Result<Map> getUserList(int page, int pageSize,String role,String uid) {
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getRole, role);
        wrapper.ne(User::getUid, uid);
        // 分页
        Page<User> userPage = new Page<>(page, pageSize);
        // 查询
        IPage<User> userIPage = userMapper.selectPage(userPage, wrapper);
        if (userIPage.getTotal() > 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("list", userIPage.getRecords());
            map.put("total", userIPage.getTotal());
            return Result.success(map);
        }
        return Result.fail("用户列表为空");
    }
}
